package com.darrenchan.course05

import scala.collection.mutable.ListBuffer

object ListApp extends App {
  //Nil就是一个空的List
  val l = List(1, 2, 3, 4, 5)
  println(l.head)
  println(l.tail)

  val l2 = 1 :: Nil
  println(l2)

  val l3 = 2 :: l2
  println(l3)

  val l4 = 1 :: 2 :: 3 :: Nil
  println(l4)

  val ll = new ListBuffer[Int]()
  ll += 1
  ll += (2, 3, 4)
  ll ++= List(7, 7, 7, 7)

  println(ll)

  ll -= 4
  println(ll)
  ll -= (2, 3)
  println(ll)

  ll --= List(7, 7, 7)
  println(ll)


  def sum(nums: Int*): Int = {
    if (nums.length == 0) {
      0
    } else {
      nums.head + sum(nums.tail: _*)
    }
  }

  def sum2(nums: List[Int]): Int = {
    if (nums.length == 0) {
      0
    } else {
      nums.head + sum(nums.tail: _*)
    }
  }

  println(sum(1, 2, 3, 4, 5))

  println(sum2(List(1, 2, 3, 4, 5)))
}
